﻿import QtQuick 2.12
import QtQuick.Controls 2.12
import QtQuick.Layouts 1.12
import SMD 1.0

ColumnLayout{
    property int __spliterLineHalfWidth: 1

    RowLayout{
        spacing: 20
        Label{
            text: qsTr("SensorTrayMap")
        }
        Button{
            text: qsTr("Reset")
            implicitHeight: 30
            enabled: stateMachine.currentState == SMD.Idle || stateMachine.currentState == SMD.Ready
            onClicked: {
                sensorTrayMap.reset()
            }
        }
    }
    TableView{
        id: sensorTrayMapView
        implicitWidth: Math.min(root.tableMaxWidth, sensorTrayConfig.columns * root.cellMaxSize)
        implicitHeight: Math.min(root.tableMaxHeight, sensorTrayConfig.rows * root.cellMaxSize)
        model: sensorTrayMap
        delegate: Rectangle{
            implicitWidth: sensorTrayMapView.width / sensorTrayConfig.columns
            implicitHeight: sensorTrayMapView.height / sensorTrayConfig.rows
            border.width: 1
            opacity: {
                if(status == "DispPrOk"){
                    return 0.33
                }
                else if(status == "DispenseOk"){
                    return 0.66
                }else{
                    return 1
                }
            }
            color: {
                if(status == "Init"){
                    return "gray"
                }
                else if(status == "DispPrOk" || status == "DispenseOk" || status == "DispCheckOk"){
                    return "yellow"
                }else if(status == "DispPrNg" || status == "DispenseNg" || status == "DispCheckNg" ){
                    return "red"
                }else{
                    return "blue"
                }
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                ToolTip{
                    text: status
                    visible: parent.containsMouse
                }
            }

        }
        Repeater{
            model: sensorTrayConfig.unitColumns - 1
            Rectangle{
                x: sensorTrayMapView.implicitWidth / sensorTrayConfig.unitColumns * (modelData + 1) - __spliterLineHalfWidth
                y: 0
                z: 10
                width: __spliterLineHalfWidth * 2
                height: sensorTrayMapView.implicitHeight
                color: "pink"
            }
        }
        Repeater{
            model: sensorTrayConfig.unitRows - 1
            Rectangle{
                x: 0
                y: sensorTrayMapView.implicitHeight / sensorTrayConfig.unitRows * (modelData + 1) - __spliterLineHalfWidth
                z: 10
                width: sensorTrayMapView.implicitWidth
                height: __spliterLineHalfWidth * 2
                color: "pink"
            }
        }
    }

    RowLayout{
        spacing: 20
        Label{
            text: qsTr("LensTrayMap")
        }
        Button{
            text: qsTr("Reset")
            implicitHeight: 30
            // TMP
//            enabled: stateMachine.currentState == SMD.Idle || stateMachine.currentState == SMD.Ready
            onClicked: {
                lensTrayMap.reset()
            }
        }
    }
    TableView{
        id: lensTrayMapView
        implicitWidth: Math.min(root.tableMaxWidth, lensTrayConfig.columns * root.cellMaxSize)
        implicitHeight: Math.min(root.tableMaxHeight, lensTrayConfig.rows * root.cellMaxSize)
        model: lensTrayMap
        delegate: Rectangle{
            implicitWidth: lensTrayMapView.width / lensTrayConfig.columns
            implicitHeight: lensTrayMapView.height / lensTrayConfig.rows
            border.width: 1
            opacity: {
                if(status == "PrOk"){
                    return 0.5
                }else{
                    return 1
                }
            }
            color: {
                if(status == "Init" ){
                    return "gray"
                } else if(status == "PrOk" || status == "InUse"){
                    return "limeGreen"
                } else if(status == "PrNg" || status == "PickFailed" || status == "Lost" ||
                          status == "UplookFailed" ){
                    return "red"
                } else{
                    return "blue"
                }
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                ToolTip{
                    text: status
                    visible: parent.containsMouse
                }
            }
        }
        Repeater{
            model: lensTrayConfig.unitColumns - 1
            Rectangle{
                x: lensTrayMapView.implicitWidth / lensTrayConfig.unitColumns * (modelData + 1) - __spliterLineHalfWidth
                y: 0
                z: 10
                width: __spliterLineHalfWidth * 2
                height: lensTrayMapView.implicitHeight
                color: "pink"
            }
        }
        Repeater{
            model: lensTrayConfig.unitRows - 1
            Rectangle{
                x: 0
                y: lensTrayMapView.implicitHeight / lensTrayConfig.unitRows * (modelData + 1) - __spliterLineHalfWidth
                z: 10
                width: lensTrayMapView.implicitWidth
                height: __spliterLineHalfWidth * 2
                color: "pink"
            }
        }
    }

    RowLayout{
        spacing: 20
        Label{
            text: qsTr("RejectTrayMap")
        }
        Button{
            text: qsTr("Reset")
            implicitHeight: 30
            enabled: stateMachine.currentState == SMD.Idle || stateMachine.currentState == SMD.Ready
            onClicked: {
                rejectTrayMap.reset()
            }
        }
    }
    TableView{
        id: rejectTrayMapView
        implicitWidth: Math.min(root.tableMaxWidth, sensorTrayConfig.columns * root.cellMaxSize)
        implicitHeight: Math.min(root.tableMaxHeight, sensorTrayConfig.rows * root.cellMaxSize)
        model: rejectTrayMap
        delegate: Rectangle{
            implicitWidth: rejectTrayMapView.width / sensorTrayConfig.columns
            implicitHeight: rejectTrayMapView.height / sensorTrayConfig.rows
            border.width: 1
            opacity: {
                if(status == "BondPrOk"){
                    return 0.33
                }
                else if(status == "Bonded"){
                    return 0.66
                }else{
                    return 1
                }
            }
            color: {
                if(status == "Init" ){
                    return "gray"
                } else if(status == "DispCheckOk"){
                    return "yellow"
                } else if(status == "BondPrOk" || status == "Bonded" || status == "Cured" || status == "PostBondOk"){
                    return "limeGreen"
                } else if(status == "DispPrNg" || status == "DispenseNg" || status == "DispCheckNg" ||
                          status == "BondPrNg" || status == "PostBondPrNg" || status == "PostBondNg"){
                    return "red"
                } else{
                    return "blue"
                }
            }
            MouseArea{
                anchors.fill: parent
                hoverEnabled: true
                ToolTip{
                    text: status
                    visible: parent.containsMouse
                }
            }
        }
        Repeater{
            model: sensorTrayConfig.unitColumns - 1
            Rectangle{
                x: rejectTrayMapView.implicitWidth / sensorTrayConfig.unitColumns * (modelData + 1) - __spliterLineHalfWidth
                y: 0
                z: 10
                width: __spliterLineHalfWidth * 2
                height: rejectTrayMapView.implicitHeight
                color: "pink"
            }
        }
        Repeater{
            model: sensorTrayConfig.unitRows - 1
            Rectangle{
                x: 0
                y: rejectTrayMapView.implicitHeight / sensorTrayConfig.unitRows * (modelData + 1) - __spliterLineHalfWidth
                z: 10
                width: rejectTrayMapView.implicitWidth
                height: __spliterLineHalfWidth * 2
                color: "pink"
            }
        }
    }

}
